package net.yla.smartbus.core.util.mcl;

import java.util.Date;

/**
 * @Author : 默苍离
 * @Create_Date : 2020/11/06/14:52
 * @Pack_name : net.yla.smartbus.module.oc.check.util : HMS
 * *****************************************************
 * 注释:2个时间相减获取 时分秒
 */


public class HMS {


    /**
     * 获取时
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Long H(Date date1, Date date2) {

        long dateTime = date1.getTime() - date2.getTime();
        long h = dateTime / (1000 * 60 * 60);

        return h;
    }


    /**
     * 获取分
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Long M(Date date1, Date date2) {

        long dateTime = date1.getTime() - date2.getTime();
        long m = dateTime % (1000 * 60 * 60) / (1000 * 60);

        return m;
    }


    /**
     * 获取秒
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Long S(Date date1, Date date2) {

        long dateTime = date1.getTime() - date2.getTime();
        long s = dateTime % (1000 * 60 * 60) % (1000 * 60) / (1000);

        return s;
    }


    /**
     * 获取时分秒
     *
     * @param date1
     * @param date2
     * @return
     */
    public static String HMS(Date date1, Date date2) {

        long dateTime = date1.getTime() - date2.getTime();
        long h = dateTime / (1000 * 60 * 60);
        long m = dateTime % (1000 * 60 * 60) / (1000 * 60);
        long s = dateTime % (1000 * 60 * 60) % (1000 * 60) / (1000);

        if (h > 0 && m > 0 && s > 0) {
            return h + "小时" + m + "分钟" + s + "秒";
        } else if (h == 0 && m == 0 && s > 0) {
            return s + "秒";
        } else if (h == 0 && m == 0 && s > 0) {
            return s + "秒";
        } else if (h == 0 && m == 0 && s > 0) {
            return s + "秒";
        } else if (h == 0 && m == 0 && s > 0) {
            return s + "秒";
        } else if (h > 0 && m == 0 && s > 0) {
            return h + "小时" + s + "秒";
        } else if (h > 0 && m > 0 && s == 0) {
            return h + "小时" + m + "分钟";
        } else if (h > 0 && m == 0 && s == 0) {
            return h + "小时";
        } else if (h == 0 && m > 0 && s == 0) {
            return m + "分钟";
        }

        return h + "小时" + m + "分钟" + s + "秒";
    }


    /**
     * 获取小时（小数）
     *
     * @param date1
     * @param date2
     * @return
     */
    public static String H_H(Date date1, Date date2) {

        long dateTime = date1.getTime() - date2.getTime();
        long h = dateTime / (1000 * 60 * 60);
        long m = dateTime % (1000 * 60 * 60) / (1000 * 60);
        //获取分钟小数
        //double mm = Math.round((m / 60.0) * 10) * 0.1;
        String mm = String.format("%.1f", m / 60.0);
        double v = Double.parseDouble(mm);
        long s = dateTime % (1000 * 60 * 60) % (1000 * 60) / (1000);

        if (h > 0 && v > 0.0 && v >= 0.1) {
            return (h + v) + "小时";
        } else if (h > 0 && v > 0.0 && v < 0.1) {
            return (h + 0.1) + "小时";
        } else if (h > 0 && v == 0.0 && s > 0) {
            return (h) + "小时";
        } else if (h > 0 && v == 0.0 && s == 0) {
            return h + "小时";
        } else if (h == 0 && v > 0.0 && s > 0) {
            return mm + "小时";
        } else if (h == 0 && v > 0.0 && s == 0) {
            return mm + "小时";
        } else if (h == 0 && v == 0.0 && s > 0) {
            return 0.1 + "小时";
        } else {
            return 0.1 + "小时";
        }
    }



    /**
     * 获取小时（小数）(大于30分钟用小时表示)
     *
     * @param date1
     * @param date2
     * @return
     */
    public static String H_M(Date date1, Date date2) {

        long dateTime = date1.getTime() - date2.getTime();

        long h = dateTime / (1000 * 60 * 60);
        long m = dateTime % (1000 * 60 * 60) / (1000 * 60);

        //获取分钟小数
        String mm = String.format("%.1f", m / 60.0);
        double v = Double.parseDouble(mm);

        //秒
        long s = dateTime % (1000 * 60 * 60) % (1000 * 60) / (1000);

        //30分钟毫秒值
        long l = 30 * 60 * 1000;

        if (dateTime > l) {
            return (h + v) + "小时";
        } else {
            if (m == 0) {
                return 1 + "分钟";
            } else {
                return m + "分钟";
            }
        }

    }


}
